package com.ark.service.impl;

import com.ark.constant.MessageConstant;
import com.ark.constant.StatusConstant;
import com.ark.dto.SetmealDTO;
import com.ark.dto.SetmealPageQueryDTO;
import com.ark.entity.Dish;
import com.ark.entity.Setmeal;
import com.ark.entity.SetmealDish;
import com.ark.exception.DeletionNotAllowedException;
import com.ark.exception.SetmealEnableFailedException;
import com.ark.mapper.DishMapper;
import com.ark.mapper.SetmealDishMapper;
import com.ark.mapper.SetmealMapper;
import com.ark.result.PageResult;
import com.ark.service.SetmealService;
import com.ark.vo.DishItemVO;
import com.ark.vo.SetmealVO;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

@Service
public class SetmealServiceImpl implements SetmealService {

    private final SetmealMapper setmealMapper;

    private final SetmealDishMapper setmealDishMapper;
    private final DishMapper dishMapper;

    public SetmealServiceImpl(SetmealMapper setmealMapper, SetmealDishMapper setmealDishMapper, DishMapper dishMapper) {
        this.setmealMapper = setmealMapper;
        this.setmealDishMapper = setmealDishMapper;
        this.dishMapper = dishMapper;
    }

    @Override
    public PageResult<SetmealVO> pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page = setmealMapper.pageQuery(setmealPageQueryDTO);
        return new PageResult<>(page.getTotal(),page.getResult());
    }

    @Override
    @Transactional
    public void saveWithDish(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);

        //向套餐表插入数据
        setmealMapper.insert(setmeal);

        //获取生成的套餐id
        Long id = setmeal.getId();

        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(id));

        //保存套餐和菜品的关联关系
        setmealDishMapper.insert(setmealDishes);
    }

    @Transactional
    @Override
    public void deleteBatch(List<Long> ids) {
        //起售中的套餐不能删除
        List<Setmeal> setmeals = setmealMapper.selectBatchIds(ids);
        setmeals.forEach(setmeal -> {
            if (Objects.equals(setmeal.getStatus(), StatusConstant.ENABLE)){
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        });
        //删除套餐表中的数据
        setmealMapper.deleteByIds(ids);
        //删除套餐菜品关系表中的数据
        setmealDishMapper.deleteBySetmealIds(ids);
    }

    @Override
    public SetmealVO getByIdWithDish(Long id) {
        Setmeal setmeal = setmealMapper.selectById(id);
        List<SetmealDish> setmealDishes = setmealDishMapper.getBySetmealId(id);
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal,setmealVO);
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }

    @Override
    @Transactional
    public void update(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        //1.修改套餐表，执行update
        setmealMapper.updateById(setmeal);
        //套餐id
        Long setmealId = setmealDTO.getId();

        //2.删除套餐和菜品的关联关系
        setmealDishMapper.deleteBySetmealId(setmealId);

        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(setmealId));

        //3.重新插入套餐和菜品的关联关系，操作setmeal_dish表
        setmealDishMapper.insert(setmealDishes);
    }

    @Override
    public void startOrStop(Integer status, Long id) {
        //判断套餐类是否有停售菜品，有停售菜品则无法起售
        List<Dish> dishList = dishMapper.getBySetmealId(id);
        if (dishList!=null&&!dishList.isEmpty()){
            dishList.forEach(dish -> {
                if (Objects.equals(dish.getStatus(), StatusConstant.DISABLE)){
                    throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                }
            });
        }
        Setmeal setmeal = Setmeal.builder()
                .id(id)
                .status(status)
                .build();
        setmealMapper.updateById(setmeal);
    }

    @Override
    public List<Setmeal> list(Setmeal setmeal) {
        return setmealMapper.list(setmeal);

    }

    @Override
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
